﻿using System.Collections.Generic;

namespace HurricanePackage
{

    public delegate void BroadcastCallDel(params object[] _objs);

    /// <summary>
    /// 带有参数的消息委托
    /// </summary>
    public class BroadcastCallBack : ICacheable
    {
        private List<BroadcastCallDel> m_callBackList;
        public List<BroadcastCallDel> CallBackList => m_callBackList;

        public  int CallCount => m_callBackList?.Count ?? 0;

        public BroadcastCallDel this[int _index]
        {
            get
            {
                if (m_callBackList != null && (_index >= 0 && _index < m_callBackList.Count))
                    return m_callBackList[_index];
                else
                    return null;
            }

        }


        public BroadcastCallBack(List<BroadcastCallDel> _delList)
        {
            if (_delList != null)
                m_callBackList = _delList;
        }

        public void Init()
        {
            if (m_callBackList == null)
                m_callBackList = new List<BroadcastCallDel>();
        }

        public void Reset()
        {
            m_callBackList?.Clear();
        }

        public void Discard()
        {
            m_callBackList?.Clear();
            m_callBackList = null;
        }
        /// <summary>
        /// 添加委托,重复地址的则忽略
        /// </summary>
        /// <param name="_del"></param>
        public void AddCall(BroadcastCallDel _del)
        {
            if(m_callBackList == null)
                return;
            if(!m_callBackList.Contains(_del))
                m_callBackList.Add(_del);
        }
        /// <summary>
        /// 添加委托列表(覆盖)
        /// </summary>
        /// <param name="_call"></param>
        public void AddCall(BroadcastCallBack _call)
        {
            if(m_callBackList == null || _call == null || _call.CallBackList== null)
                return;
            m_callBackList.Clear();
            m_callBackList.AddRange(_call.CallBackList);
        }
        /// <summary>
        /// 移除委托
        /// </summary>
        /// <param name="_del"></param>
        public void RemoveCall(BroadcastCallDel _del)
        {
            m_callBackList?.Remove(_del);
        }
    }
}